package com.wego.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wego.bean.PageBean;
import com.wego.domain.entity.GameCategory;
import com.wego.domain.query.GameCategoryQuery;
import com.wego.mapper.GameCategoryMapper;
import com.wego.service.GameCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class GameCategoryServiceImpl implements GameCategoryService {
@Autowired
private GameCategoryMapper gameCategoryMapper;

@Override
public PageBean<GameCategory> getPage(GameCategoryQuery gameCategoryQuery) {
    final Integer pageNum = gameCategoryQuery.getPageNum();
    final Integer pageSize = gameCategoryQuery.getPageSize();
    final Page<GameCategory> page = PageHelper.startPage(pageNum, pageSize);
        gameCategoryMapper.selectWithCondition(gameCategoryQuery);
        return PageBean.page2pageBean(page);
    }

    @Override
    public GameCategory getById(Integer id) {
        return gameCategoryMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<GameCategory> getAll() {
        return gameCategoryMapper.selectAll();
    }

    @Override
    public int insert(GameCategory gameCategory) {
        if (gameCategory == null) {
            throw new IllegalArgumentException("GameCategory cannot be null");
        }
        if (gameCategory.getName() == null) {
            throw new IllegalArgumentException("Name cannot be null");
        }
        //设置更新时间为当前时间
        gameCategory.setCreateTime(LocalDateTime.now());
        gameCategory.setUpdateTime(LocalDateTime.now());
        return gameCategoryMapper.insertSelective(gameCategory);
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteByIds(List<Integer> ids) {
        return gameCategoryMapper.deleteByPrimaryKeyIn(ids);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, rollbackFor = Exception.class)
    public int batchInsert(List<GameCategory> gameCategoryList) {
        return gameCategoryMapper.batchInsert(gameCategoryList);
    }

    @Override
    public int update(GameCategory gameCategory) {
        if (gameCategory == null) {
            throw new IllegalArgumentException("GameCategory cannot be null");
        }
        if (gameCategory.getId() == null) {
            throw new IllegalArgumentException("ID cannot be null");
        }
        gameCategory.setUpdateTime(LocalDateTime.now());
        return gameCategoryMapper.updateByPrimaryKeySelective(gameCategory);
    }
}
